package org.leviatan.definator.core.admin;

import java.util.Collection;
import java.util.HashMap;
import java.util.Vector;

import org.leviatan.definator.core.model.Field;
import org.leviatan.definator.core.model.Record;
import org.leviatan.definator.core.model.Table;
import org.leviatan.definator.core.registry.Registry;

/* Controlloing wrapper around Registry */
public abstract class RegistryHandler {
	
	
	public static HashMap getFieldModels() {
		return Registry.getInstance().getFieldModels();
	}
	
	public static HashMap getRecordModels() {
		return Registry.getInstance().getRecordModels();
	}
	
	public static HashMap getTableModels() {
		return Registry.getInstance().getTableModels();
	}
	
	public static Field getFieldModel(String id) {
		return (Field) Registry.getInstance().getFieldModels().get(id);
	}
	
	public static Record getRecordModel(String id) {
		return (Record) Registry.getInstance().getRecordModels().get(id);
	}
	
	public static Table getTableModel(String id) {
		return (Table) Registry.getInstance().getTableModels().get(id);
	}
	
	public static Collection getRecordsFieldIds(String recId) {
		return (Vector) Registry.getInstance().getRec2FldsMap().get(recId);
	}
	
	public static Collection getTablesRecordIds(String tblId) {
		return (Vector) Registry.getInstance().getTbl2RecsMap().get(tblId);
	}
	
	public static String getFieldsRecordId(String fldId) {
		return Registry.getInstance().getFld2RecsMap().get(fldId);
	}
	
	public static String getRecordsTableId(String recId) {
		return Registry.getInstance().getRec2TblMap().get(recId);
	}
	
	
	///////////////////////////////////////////////////////////////////////////
	public static void prepareFld2RecMap() {
		Registry.getInstance().fillFld2RecMap();
	}
	
	public static void prepareRec2FldsMap() {
		Registry.getInstance().fillRec2FldsMap();
	}
	
	public static void prepareRec2TblMap() {
		Registry.getInstance().fillRec2TblMap();
	}
	
	public static void prepareTbl2RecsMap() {
		Registry.getInstance().fillTbl2RecsMap();
	}
	
	public static void loadTableModels() {
		Registry.getInstance().loadTableModels();
	}
	
	public static void loadRecordModels() {
		Registry.getInstance().loadRecordModels();
	}
	
	public static void loadFieldModels() {
		Registry.getInstance().loadFieldModels();
	}
	
	///////////////////////////////////////////////////////////////////////////
	/* ..uses singleton Registry's singletion feature */
	public static Table addTableModel(Table tbl) {
		Registry.getInstance().addTableModel(tbl);
		return tbl;
	}
	
	public static Record addRecordModel(Record rec) {
		Registry.getInstance().addRecordModel(rec);
		if (rec.getTableRef() != null && !rec.getTableRef().equals(""))
			Registry.getInstance().addRecord2TableMapping(rec.getId(), rec.getTableRef());
		return rec;
	}
	
	public static Field addFieldModel(Field fld) {
		Registry.getInstance().addFieldModel(fld);
		if (fld.getRecordRef() != null && !fld.getRecordRef().equals(""))
			Registry.getInstance().addField2RecordMapping(fld.getId(), fld.getRecordRef());
		return fld;
	}
	
	public static boolean removeTableModel(String tblId) {
		if (Registry.getInstance().getTableModels().remove(tblId) == null)
			return false;
		else {
			Registry.getInstance().removeTable2RecordMapping(tblId);
			return true;
		}
	}
	
	public static boolean removeRecordModel(String recId) {
		if (Registry.getInstance().getRecordModels().remove(recId) == null)
			return false;
		else {
			Registry.getInstance().removeRecord2FieldMapping(recId);
			return true;
		}
	}
	
	public static boolean removeFieldModel(String fldId) {
		if (Registry.getInstance().getFieldModels().remove(fldId) == null)
			return false;
		else {
			Registry.getInstance().removeField2RecordMapping(fldId);
			return true;
		}
	}
	
	
	public static void saveTableModels() {
		Registry.getInstance().saveTableModelsToFile();
	}
	
	public static void saveRecordModels() {
		Registry.getInstance().saveRecordModelsToFile();
	}
	
	public static void saveFieldModels() {
		Registry.getInstance().saveFieldModelsToFile();
	}
	
	
	
	/* To be implemented later on ********************************************/
	
	public abstract void clearRegistry();
	
	public abstract boolean removeTableModelCascading(String tblId);
	public abstract boolean removeRecordModelCascading(String recId);
	public abstract void duplicateModel();
	public abstract void moveModel();
	
	public abstract void saveTbl2RecsMap();
	public abstract void saveRec2FldsMap();
	public abstract void saveFld2RecMap();
	public abstract void saveRec2TblMap();
	
	public abstract Collection getTanglingRecords();
	public abstract Collection getTanglingFields();
	
	
}
